//
//  ICMPropertyManager.m
//  ICrEAM
//
//  Created by rainimute on 16/11/10.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMPropertyManager.h"
#import "ICMTitleTextFeildView.h"
#import "ICMTitleTextView.h"

@implementation ICMPropertyManager

+ (NSArray *)getAllPropertiesWithClass:(Class)modelClass
{
    u_int count;
    objc_property_t *properties = class_copyPropertyList(modelClass, &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++)
    {
        const char* propertyName = property_getName(properties[i]);
        [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
    }
    free(properties);
    [propertiesArray removeObjectsInArray:@[@"hash",@"superclass",@"description",@"debugDescription"]];
    return propertiesArray;
}

+ (void)setAllValueForEmpty:(id)object{
    NSArray *propertyNames = [self getAllPropertiesWithClass:[object class]];
    for (NSString *propertyName in propertyNames) {
        [object setValue:@"" forKeyPath:propertyName];
    }
}

+ (void)setAllNilVauleToEntptyString:(id)object{
    
    NSArray * expNames = @[@"hash",@"superclass",@"description",@"debugDescription"];
    
    u_int count;
    objc_property_t *properties = class_copyPropertyList([object class], &count);
    for (int i = 0; i<count; i++)
    {
        const char* propertyName = property_getName(properties[i]);
        NSString *propertyNameString = [NSString stringWithUTF8String: propertyName];
        if (![expNames containsObject:propertyNameString]) {
            id value = [object valueForKeyPath:propertyNameString];
            if (!value) {
                const char* propertyAttribute = property_getAttributes(properties[i]);
                NSString *propertyType = [NSString stringWithUTF8String:propertyAttribute];
                if ([propertyType isEqualToString:@"NSString"]) {
                    [object setValue:@"" forKeyPath:propertyNameString];
                }else if ([propertyType isEqualToString:@"NSNumber"]){
                    [object setValue:@(0) forKeyPath:propertyNameString];
                }
            }
        }
    }
    free(properties);
}

+ (BOOL)compairObject:(id)object withOtherObject:(id)otherObject{
    if (![NSStringFromClass([object class]) isEqualToString:NSStringFromClass([otherObject class])]) {
        return NO;
    }
    NSArray * expNames = @[@"hash",@"superclass",@"description",@"debugDescription"];
    
    u_int count;
    objc_property_t *properties = class_copyPropertyList([object class], &count);
    objc_property_t *propertiesOther = class_copyPropertyList([otherObject class], &count);
    BOOL isSame = YES;
    for (int i = 0; i<count; i++)
    {
        const char* propertyName = property_getName(properties[i]);
        NSString *propertyNameString = [NSString stringWithUTF8String: propertyName];
        if (![expNames containsObject:propertyNameString]) {
            id value = [object valueForKeyPath:propertyNameString];
            id valueOther = [otherObject valueForKeyPath:propertyNameString];
            
            const char* propertyAttribute = property_getAttributes(properties[i]);
            NSString *propertyType = [NSString stringWithCString:propertyAttribute encoding:NSUTF8StringEncoding];

            if ((!value && valueOther)||(value && !valueOther)) {
                isSame = NO;
            }
            if ([propertyType containsString:@"NSString"]) {
                if((![value isEqualToString:valueOther])&& value!=valueOther){
                    isSame = NO;
                }
            }else if ([propertyType containsString:@"NSNumber"]){
                if([value integerValue] != [valueOther integerValue]){
                    isSame = NO;
                }
            }else if ([propertyType containsString:@"float"]){
                if (value != valueOther){
                    isSame = NO;
                }
            }else if ([propertyType containsString:@"BOOL"]){
                if (value != valueOther){
                    isSame = NO;
                }
            }
        }
    }
    free(properties);
    free(propertiesOther);
    return isSame;
}

+ (BOOL)isHaveTextInTextField:(UIView*)aView{
    if (!aView) {
        return NO;
    }
    BOOL haveText = NO;
    u_int count;
    objc_property_t *properties = class_copyPropertyList([aView class], &count);
    for (int i = 0; i<count; i++)
    {
        const char* propertyName = property_getName(properties[i]);
        NSString *propertyNameString = [NSString stringWithUTF8String: propertyName];
        const char* propertyAttribute = property_getAttributes(properties[i]);
        NSString *propertyType = [NSString stringWithCString:propertyAttribute encoding:NSUTF8StringEncoding];
        if ([propertyType containsString:@"ICMTitleTextFeildView"]||[propertyType containsString:@"ICMTitleTextView"]) {
            NSString *text = ((ICMTitleTextFeildView*)[aView valueForKeyPath:propertyNameString]).contentText;
            NSMutableString *textMutable = [text mutableCopy];
            NSInteger length = textMutable.length;
            for (NSInteger i=0; i<length; i++) {
                NSRange emptyRange = [textMutable rangeOfString:@" "];
                if (emptyRange.length>0) {
                    [textMutable replaceCharactersInRange:emptyRange withString:@""];
                }
                NSRange returnRange = [textMutable rangeOfString:@"\n"];
                if (returnRange.length>0) {
                    [textMutable replaceCharactersInRange:returnRange withString:@""];
                }
            }
            if (![textMutable isEqualToString:@""] && textMutable) {
                if ([NSStringFromClass([aView class]) isEqualToString:@"ICMAppointmentBasicInfoView"]&&[propertyNameString isEqualToString: @"appointmentTypeView"]) {
                    
                }else{
                    haveText = YES;
                }
            }
        }
    }
    free(properties);
    return haveText;
}

@end






